import pygame
from .const import SIZE

class BGSprite(pygame.sprite.Sprite):
    def __init__(self, name, pos_y, speed=5):
        super().__init__()
        self.image = pygame.image.load(f"./image/{name}")
        self.rect = self.image.get_rect()
        self.rect.top = pos_y
        self.speed = speed

    def update(self):
        self.rect.top += self.speed
        if self.rect.top >= SIZE[1]:
            # 需要注意 n就是总共有几个背景精灵
            n = 3
            self.rect.top = -SIZE[1] * (n - 1)



class BGManage:
    def __init__(self, gm):
        self.bg_group = pygame.sprite.Group()

        self.bg1 = BGSprite("bg.png", 0)
        self.bg1.add(self.bg_group)
        self.bg2 = BGSprite("bg2.jpeg", -600)
        self.bg2.add(self.bg_group)
        self.bg3 = BGSprite("bg3.jpeg", -1200)
        self.bg3.add(self.bg_group)

        self.gm = gm

    def update(self):
        self.bg_group.draw(self.gm.screen)
        if self.gm.state == "gaming":
            self.bg_group.update()

